package com.bunde;

import com.bunde.sax.XMLParseUtils;
import com.bunde.sax.XmlParseHandler;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    public static void main(String[] args) throws Exception {
        // 读入xml文件
        String filename = "F:\\tmp\\learn_repo\\sax_demo\\src\\main\\resources\\user.xml";
        InputStream in = new FileInputStream(filename);
        System.out.println();
        // 1. 创建解析器处理器，继承默认的DefaultHandler
        XmlParseHandler xmlParseHandler = new XmlParseHandler();
        // 2. SAX工厂实例
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        // 3. 创建SAX解析器实例
        SAXParser saxParser = null;
        saxParser = saxParserFactory.newSAXParser();

        // 4. 解析
        saxParser.parse(in, xmlParseHandler);
        in.close();

        // 查看解析结果
        System.out.println(xmlParseHandler.getUsers());
    }
}

